package thesis.filesharing.web.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import thesis.filesharing.bo.LecturerBO;
import thesis.filesharing.common.util.XMLUtil;
import thesis.filesharing.model.Lecturer;
import thesis.filesharing.resources.Constants;
import thesis.filesharing.web.util.Util;
import thesis.filesharing.web.validator.CreateLecturerValidator;
import thesis.filesharing.web.viewmodel.CreateLecturerViewModel;

@Controller
public class LecturerController {
	
	@Autowired
	private CreateLecturerValidator createLecturerValidator;
	
	@Autowired
	private LecturerBO lecturerBO;
	
	List<String> departmentNames = XMLUtil.readDataNodes(Constants.DEPARTMENTS_FILE_PATH, "department-name");
	
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/createLecturer", method = RequestMethod.GET)
	public String initCreateLecturerForm(ModelMap model) {
		CreateLecturerViewModel createLecturerViewModel = new CreateLecturerViewModel();
		model.addAttribute("createLecturerViewModel", createLecturerViewModel);
		return "admin/createLecturer";
	}
	
	@ModelAttribute("departments")
	public Map<Integer, String> populateDepartments() {
		Map<Integer, String> departments = new LinkedHashMap<Integer, String>();
		for (int i = 0; i < departmentNames.size(); i++) {
			departments.put(i, departmentNames.get(i));
		}
		return departments;
	}
	
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/createLecturer", method = RequestMethod.POST)
	public ModelAndView processCreateLecturerSubmit(
			@ModelAttribute("createLecturerViewModel") CreateLecturerViewModel createLecturerViewModel,
			BindingResult bindingResult, SessionStatus status) {
		
		createLecturerValidator.validate(createLecturerViewModel, bindingResult);
		
		if (bindingResult.hasErrors()) {
			return new ModelAndView("admin/createLecturer");
		} else {
			status.setComplete();
			
			boolean isActive = true;
			String department = departmentNames.get(Integer.parseInt(createLecturerViewModel.getDepartment()));
			Lecturer lecturer = new Lecturer(createLecturerViewModel.getLecturerId(), createLecturerViewModel.getLecturerName(), 
					createLecturerViewModel.getLecturerEmail(), Util.generatePassword(), department, isActive);
			lecturerBO.saveLecturer(lecturer, createLecturerViewModel.getIsAdmin());
			
			return new ModelAndView(new RedirectView("admin/listLecturer"));
		}
	}
}
